package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List; 
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;

import cn.com.api.service.*;

import cn.com.util.*;


public class U_labelBaseController {

	private static Logger logger = Logger.getLogger(U_labelBaseController.class);
	
	@Autowired
	private U_labelService u_labelService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_label-list")
	public ModelAndView u_label_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_label_list.jsp");
		
		List<U_label> u_labels = u_labelService.findU_labels();
		mv.addObject("u_labels", u_labels);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_label-add-pre")
	public ModelAndView u_label_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_label_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			U_label u_label = u_labelService.findU_labelById(Integer.valueOf(id));
			mv.addObject("u_label",u_label);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_label-add")
	public void u_label_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String label_name = request.getParameter("label_name");
		String sort = request.getParameter("sort");
		String parent_id = request.getParameter("parent_id");
		String create_time = request.getParameter("create_time");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			U_label u_label = new U_label();
            /*
		    u_label.setId(id);
		    u_label.setLabel_name(label_name);
		    u_label.setSort(sort);
		    u_label.setParent_id(parent_id);
		    u_label.setCreate_time(create_time);
	        */
			u_labelService.insertU_label(u_label);
		    //
            map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
		    map.put(Util.MESSAGE, "添加成功！");
        }
		//edit
		else{
			U_label u_label = u_labelService.findU_labelById(Integer.valueOf(id));
	        /*
		    u_label.setLabel_name(label_name);
		    u_label.setSort(sort);
		    u_label.setParent_id(parent_id);
		    u_label.setCreate_time(create_time);
			*/
			u_labelService.updateU_label(u_label);
		    //
            map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
		    map.put(Util.MESSAGE, "修改成功！");
        }
		//
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_label-delete")
	public void u_label_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	u_labelService.deleteU_label(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
			map.put(Util.MESSAGE, "删除成功！");
		}

		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



